package com.koala.learn.reflectlearn;

import cn.hutool.core.util.ReflectUtil;
import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author yantingrui
 * @date 2021/12/8   15:58
 */
public class Test1 {


    public Map<String, Object> beanToMap(Object obj) {
        Field[] declaredFields = obj.getClass().getDeclaredFields();
        TreeMap<String, Object> map = new TreeMap<String, Object>();
        for (Field item : declaredFields) {
            String fieldName = ReflectUtil.getFieldName(item);
            Object fieldValue = ReflectUtil.getFieldValue(obj, fieldName);
            map.put(fieldName, fieldValue);
        }
        return map;
    }


    /**
     *
     */
    @Test
    public void test34() {
        String s = "12";
        Map<String, Object> asd = beanToMap(s);
        System.out.println(asd);
    }

    /**
     *
     */
    @Test
    public void test33() {
        Car car = new Car("A", "benci");
        Map<String, Object> stringObjectMap = beanToMap(car);
        System.out.println(JSON.toJSONString(stringObjectMap));
    }

    /**
     *
     */
    @Test
    public void test18() {
        Car car = new Car("12", "12");
        Field[] declaredFields = car.getClass().getDeclaredFields();
        for (Field item : declaredFields) {
            String fieldName = ReflectUtil.getFieldName(item);
            Object fieldValue = ReflectUtil.getFieldValue(car, fieldName);
            System.out.println(String.format("key:%s value:%s", fieldName, fieldValue));
        }

        //System.out.println(JSON.toJSONString(setConditionMap(car)));
    }

    public static Map<String, Object> setConditionMap(Object obj) {
        TreeMap<String, Object> map = new TreeMap<String, Object>();
        if (obj == null) {
            return null;
        }
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            String fieldName = field.getName();
            if (getValueByFieldName(fieldName, obj) != null) {
                map.put(fieldName, getValueByFieldName(fieldName, obj));
            }
        }
        return map;
    }

    public static Object getValueByFieldName(String fieldName, Object object) {
        String firstLetter = fieldName.substring(0, 1).toUpperCase();
        String getter = "get" + firstLetter + fieldName.substring(1);
        try {
            Method method = object.getClass().getMethod(getter);
            return method.invoke(object);
        } catch (Exception e) {
            return null;
        }
    }

    @Test
    public void test() {
        List<String> list1 = new ArrayList<>();
        list1.add("A");
        list1.add("B");

        List<String> list2 = new ArrayList<>();
        list2.add("C");
        list2.add("D");

        list1.addAll(list2);
        list1.forEach(v-> System.out.println(v.toString()));

    }

    @Test
    public void test1(){
        System.out.println("ss");
    }


}
